package br.com.dao.hibernate;

import java.lang.reflect.ParameterizedType;
import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;

import br.com.dao.GenericDAO;


public abstract class HibernateGenericDAO<T> implements GenericDAO<T> {
	private Class<T> persistentClass;
	private Session session;

	public HibernateGenericDAO(Session session) {
		this.session = session;
		this.persistentClass = (Class<T>) ( (ParameterizedType) getClass().getGenericSuperclass() ).getActualTypeArguments()[0];
	}

	public Session getSession() {
		return this.session;
	}

	public T pesquisarPorId(Serializable id) {
		return (T) this.session.get(this.persistentClass, id);
	}

	public List<T> listarTodos() {
		return this.session.createCriteria(this.persistentClass).list();
	}

	public T salvar(T entidade) {
		return (T) this.session.merge(entidade);
	}

	public void excluir(T entidade) {
		this.session.delete(entidade);
	}
}

